package com.i2bt.app.friend;

import com.i2bt.common.AndroidRestContract;
import com.i2bt.common.Contract;

import com.i2bt.R;
import com.i2bt.adapter.FriendMessageListAdapter;
import com.i2bt.app.BaseActivity;
import com.i2bt.app.friend.model.FriendMessageJsonHandle;
import com.i2bt.bean.FriendBean;
import com.i2bt.util.FriendMessageUtil;
import com.i2bt.view.PopuItem;
import com.i2bt.view.PopuJar;
import com.i2bt.view.PopuJar.OnPopuItemClickListener;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

/**
 * 
 * Title : 好友信息查看活动类
 *
 * Description : 里面可以处理好友关注的信息, 也可以查看关注好友和好友发来的等等消息
 *
 * CreatedTime : 2014-8-21
 *
 * @author wuliao & kakaxi & yi
 *
 * @version 1.0
 *
 */

public class FriendMessageActivity extends BaseActivity implements OnItemClickListener, OnPopuItemClickListener{

	private ListView friendMessageListView; // 好友信息列表

	private List<FriendBean> friendMessageData; // 好友信息数组

	private FriendMessageListAdapter messageListAdapter; // 好友信息列表适配器
	
	private FriendMessageJsonHandle friendMessageJsonHandle; // 好友信息json处理器
	
	private PopuJar friendMessagePopu; // 弹出式菜单
	
	private final static int REFUSE_ITEM_ID = 1; // 弹出式菜单拒绝id;
	
	private final static int DELETE_ITEM_ID = 2; // 弹出式菜单删除id;
	
	private int mark_position; // 位置标记

	@Override
	@SuppressLint("NewApi")
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_friend_message);
		
//		makeData();
		initView();
		logic();
	}
	
	public void makeData() {
		FriendBean bean = new FriendBean();
		bean.setContent("aa");
		bean.setFlag(1);
		bean.setReceiver("aa");
		bean.setSender("ewa");
		friendMessageData = new ArrayList<FriendBean>();
		friendMessageData.add(bean);
		bean.setContent("aa");
		bean.setFlag(1);
		bean.setReceiver("aa");
		bean.setSender("ewa");
		friendMessageData.add(bean);
	}

	@Override
	protected void initView() {
		
		//好友信息列表组装
		messageListAdapter = new FriendMessageListAdapter(this,
				R.layout.item_friend_message_list, friendMessageData);
		
		friendMessageListView = (ListView) findViewById(R.id.friend_message_list);
		friendMessageListView.setAdapter(messageListAdapter);
		friendMessageListView.setOnItemClickListener(this);
		
		//初始化拒绝和删除好友信息的弹出式菜单
		PopuItem refuseItem = new PopuItem(REFUSE_ITEM_ID, "拒绝");
		refuseItem.setSticky(true);
		PopuItem deleteItem = new PopuItem(DELETE_ITEM_ID, "删除");
		deleteItem.setSticky(true);
		
		friendMessagePopu = new PopuJar(this, PopuJar.HORIZONTAL);
		friendMessagePopu.addPopuItem(refuseItem);
		friendMessagePopu.addPopuItem(deleteItem);
		friendMessagePopu.setOnPopuItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
		mark_position = position;
		friendMessagePopu.show(view);
	}
	
	/**
	 * 弹出式菜单监听器方法
	 */
	@Override
	public void onItemClick(PopuJar source, int pos, int actionId) {
		
		switch (actionId) {
		
		//点击菜单的拒绝
		case REFUSE_ITEM_ID : {
			// 标志发送请求数据在列表中的位置, 方便响应时调用
			refuseFriendMessageRequest();
		}
		
		//点击菜单的删除
		case DELETE_ITEM_ID : {
			deleteFriendMessageRequest();
		}
		
		}
		
		friendMessagePopu.dismiss();
	}
	
	@Override
	protected void logic() {
		
		if (friendMessageJsonHandle == null) {
			friendMessageJsonHandle = new FriendMessageJsonHandle(this);
		}
		
		getFriendMessageRequest();
		
	}
	
	@Override
	public void onClick(View v) {
		
	}
	
	/**
	 * 获取好友信息请求
	 */
	private void getFriendMessageRequest() {
		FriendBean friendBean = new FriendBean();
		friendBean.setAct(AndroidRestContract.GET_FRIEND_MESSAGE);
		
		friendMessageJsonHandle.postTo(AndroidRestContract.REST_FRIEND,
				friendBean);
	}
	
	/**
	 * 获取好友信息响应
	 * @param friendMessageData 好友信息数据表
	 */
	public void getFriendMessageResponse(List<FriendBean> friendMessageData) {
		this.friendMessageData = friendMessageData;
		messageListAdapter.setData(this.friendMessageData);
		messageListAdapter.notifyDataSetInvalidated();
	}
	
	/**
	 * 发送接收信息的请求
	 * @param position
	 */
	public void acceptFriendMessageRequest(int position) {
		// 标志发送请求数据在列表中的位置, 方便响应时调用
		mark_position = position;
		
		friendMessageJsonHandle.postTo(AndroidRestContract.REST_MESSAGE, 
				warpFriendMessageBean(FriendMessageUtil.ACCEPT));
	}
	
	/**
	 * 处理接受信息的响应
	 */
	public void acceptFriendMessageResponse() {
		FriendBean dataBean = friendMessageData.get(mark_position);
		dataBean.setFlag(FriendMessageUtil.ACCEPT);
		FriendMessageUtil.messageContentDecoded(dataBean);
		messageListAdapter.notifyDataSetChanged();
	}
	
	/**
	 * 发送拒绝信息的请求
	 */
	private void refuseFriendMessageRequest() {
		friendMessageJsonHandle.postTo(AndroidRestContract.REST_MESSAGE, 
				warpFriendMessageBean(FriendMessageUtil.REFUSE));
	}
	
	/**
	 * 处理拒绝信息的响应
	 */
	public void refuseFriendMessageResponse() {
		FriendBean dataBean = friendMessageData.get(mark_position);
		dataBean.setFlag(FriendMessageUtil.REFUSE);
		FriendMessageUtil.messageContentDecoded(dataBean);
		messageListAdapter.notifyDataSetChanged();
	}
	
	private void deleteFriendMessageRequest() {
		friendMessageJsonHandle.postTo(AndroidRestContract.REST_MESSAGE, 
				warpFriendMessageBean(-1));
	}
	
	/**
	 * 处理删除信息的响应
	 */
	public void deleteFriendMessageResponse() {
		friendMessageData.remove(mark_position);
		messageListAdapter.notifyDataSetChanged();
	}
	
	/**
	 * 打包信息bean, 方便发送接收和拒绝, 删除
	 * @param flag  信息标志
	 * @return
	 */
	private FriendBean warpFriendMessageBean(int flag) {
		FriendBean dataBean = friendMessageData.get(mark_position);
		FriendBean friendBean = new FriendBean();
		
		//接受, 拒绝, 删除 通用打包
		friendBean.setSender(dataBean.getSender());
		friendBean.setReceiver(dataBean.getReceiver());
		friendBean.setLongTimeFlag(dataBean.getLongTimeFlag());
		// 判断是否为接受和拒绝信息, 不是的话就打包成删除信息
		if (flag != -1) {
			friendBean.setAct(AndroidRestContract.ADD_FRIEND);
			friendBean.setFlag(flag);
			friendBean.setDate(new SimpleDateFormat("yyyy-MM-dd",
					Locale.CHINA).format(new Date()));
			FriendMessageUtil.messageContentEecoded(friendBean);
		}
		else {
			friendBean.setAct(AndroidRestContract.DELETE_FRIEND_MESSAGE);
		}
		
		return friendBean;
	}
	
	@Override
	public void backHomeOperate() {
		finish();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.friend_message_menu, menu);
		//隐藏actionbar的菜单组, 并设置后退键
//		setActionMenuGroupVisible(false);
		setActionBarBack(Contract.TITLE_FRIEND_MESSAGE_NAME);
		return true;
	}


}
